package com.shg.service.impl;

import java.util.LinkedHashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.springframework.stereotype.Service;

import com.shg.bean.Book;
import com.shg.bean.OrderItem;
import com.shg.bean.PageView;
import com.shg.bean.QueryResult;
import com.shg.bean.Shop;
import com.shg.bean.SoldBook;
import com.shg.bean.Users;
import com.shg.service.SoldBookService;

@Service
public class SoldBookServiceBean extends BasiceDaoImpl<SoldBook> implements SoldBookService {


	/**
	 * 调用该方法将会回去售出书籍的信息及其
	 * 购书人的信息
	 * @param分页工具类
	 */
	public QueryResult<SoldBook> getAppraise(PageView<SoldBook> pageView,String bookid){
		String whereSQL=" book_id=? and appraise is not null ";
		return getSoldHistory(pageView,bookid,whereSQL);
	}
	/**
	 * 获取最近的销售记录
	 */
	public QueryResult<SoldBook> getSoldRecord(PageView<SoldBook> pageView,String bookid){
		String whereSQL=" book_id=? ";
		return getSoldHistory(pageView,bookid,whereSQL);
	}
	/**
	 * 查询销售出去的历史书籍
	 * @param pageView 分页工具
	 * @param bookid  销售出去的书籍父类ID
	 * @param whereSQL  查询的条件部分
	 * @return
	 */
	private QueryResult<SoldBook> getSoldHistory(PageView<SoldBook> pageView,String bookid,String whereSQL){
		Object[] parameters=new Object[]{bookid};
		LinkedHashMap<String, String> orderBy=new LinkedHashMap<String, String>();
		orderBy.put("usertime", "desc");
		QueryResult<SoldBook> queryResult =getPagination(SoldBook.class, pageView.getFirstResult(), pageView.getMaxrecord(), whereSQL, parameters, orderBy);
		List<SoldBook> list = queryResult.getResultList();
		//获取书籍父类
		Book parent = (Book) sessionFactory.getCurrentSession().get(Book.class, bookid);
		for(SoldBook book:list){
			OrderItem item = book.getOrderItem();
			Users users = (Users) sessionFactory.getCurrentSession().get(Users.class,item.getUsers().getId());
			book.getOrderItem().setUsers(users);
			//设置父类
			book.setBook(parent);
		}
		return queryResult;
	}
	/** 
	 * 统计出畅销书
	 * 这里的统计是将某本书籍所有的销售记录都统计出来
	 */
	public List<Book> getBestSeller(String shopId,int maxResult) {
		return getStat(shopId, maxResult, SoldBook.class);
	}
	/** 
	 * 统计出畅销书(这个网站的畅销书)
	 * 这里的统计是将某本书籍所有的销售记录都统计出来
	 */
	public List<Book> getBestBook(int maxResult) {
		return getStat(null, maxResult, SoldBook.class);
	}
	/**分页查询某个店铺销售出去的所有书架
	 * @param shopid店铺的ID
	 * @param firstResult查询的其实位置
	 * @param maxResult查询的结束位置
	 */
	@SuppressWarnings("unchecked")
	public QueryResult<SoldBook> getSoldBook(String shopid,int firstResult,int maxResult){
		QueryResult<SoldBook> result=new QueryResult<SoldBook>();
		//查询店铺
		List<Shop> shop =DetachedCriteria.forClass(Shop.class)
			.add(Property.forName("id").eq(shopid))
			.getExecutableCriteria(sessionFactory.getCurrentSession()).list();
		//查询指定店铺下的所有书籍
		List<Book> book = sessionFactory.getCurrentSession().createCriteria(Book.class)
		.add(Property.forName("shop").eq(shop.get(0))).list();
		//查询售出的所有书籍
		Criteria lists=null;
		if(book.size()>0){
			lists = sessionFactory.getCurrentSession().createCriteria(SoldBook.class)
			.add(Property.forName("book").in(book))
			.setFirstResult(firstResult)
			.setMaxResults(maxResult)
			.addOrder(Order.desc("orderItem"));
		}
		result.setResultList(lists==null?null:lists.list());
		//统计总记录数
		List<Long> resultCount=null;
		if(book.size()>0){
			resultCount=sessionFactory.getCurrentSession().createCriteria(SoldBook.class)
			.add(Property.forName("book").in(book))
			.setProjection(Projections.rowCount()).list();
		}
		
		
		result.setResultCount(resultCount==null?0:resultCount.get(0));
		return result;
	}
}
